﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace Basefr.Runtime.Helpers
{
    public static class RunCmdHelper
    {
        public static bool RunAsync(string exeFile, int timeoutMilliSeconds, params string[] args)
        {
            if (!File.Exists(exeFile))
            {
                throw new ApplicationException("可执行程序文件路径无效 " + exeFile);
            }

            Task.Run(() =>
            {
                var processObj = new Process
                {
                    StartInfo =
                    {
                        FileName = exeFile,
                        Arguments = args[0]+args[1]
                    }
                };


            });

            return true;
        }

        public static bool Run(string exeFile, int timeoutMilliSeconds, params string[] args)
        {
            if (!File.Exists(exeFile))
            {
                throw new ApplicationException("可执行程序文件路径无效 " + exeFile);
            }
            var applicationArguments = $"\"{ args[0]}\" \"{ args[1]}\"";
            var processObj = new Process
            {
                StartInfo =
                {
                    FileName = exeFile,
                    Arguments = applicationArguments
                }
            };

            processObj.StartInfo.UseShellExecute = false;
            processObj.StartInfo.CreateNoWindow = true;
            processObj.StartInfo.RedirectStandardOutput = true;
            processObj.Start();

            return processObj.WaitForExit(timeoutMilliSeconds);

        }
    }
}
